<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<base href="/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>关键词添加</title>
<link href="static/templet/css/bootstrap.min.css?v=3.3.5"
	rel="stylesheet">
<link href="static/templet/css/font-awesome.min93e3.css?v=4.4.0"
	rel="stylesheet">
<link href="static/templet/css/plugins/iCheck/custom.css"
	rel="stylesheet">
<link href="static/templet/css/animate.min.css" rel="stylesheet">
<link href="static/templet/css/style.min862f.css?v=4.1.0"
	rel="stylesheet">

</head>

<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-content clearfix">
						<c:choose>
							<c:when test="${proxy == '01' }">
								<div class="form-group clearfix">
								<label class="col-sm-2 control-label tr">客户名：</label>
								<div class="col-sm-5">
									<input id="userName" name="userName" placeholder="请输入客户名"
										class="form-control" type="text">
								</div>
								</div>
								<div class="form-group clearfix">
								<span style="color: red; padding-left: 100px">客户名非必填，为下级用户添加关键词时填写其用户名。</span>
								<br>
								</div>
							</c:when>
							
							<c:otherwise>
									<input id="userName" name="userName" type="hidden">
							</c:otherwise>
						</c:choose>

						<div class="form-group clearfix">
							<label class="col-sm-2 control-label tr"><span
								style="color: red">*</span>&nbsp;搜索引擎：</label>
							<div class="col-sm-6">
								<c:forEach var="co" varStatus="i" items="${engine}">
									<label><input name="engineCode" type="checkbox"
										value="${co.code }" />${co.name}</label>
								</c:forEach>
							</div>
						</div>
						<div class="form-group clearfix">
							<label class="col-sm-2 control-label tr"><span
								style="color: red">*</span>&nbsp;上传方式：</label>
							<div class="col-sm-3">
								<select class="form-control" name="uploadType" id="uploadType"
									onchange="changeUpload(this)" data-placeholder="上传方式">
									<option value="01"
										<c:if test="${param.uploadType == '01'}">selected</c:if>>交叉上传</option>
									<option value="02"
										<c:if test="${param.uploadType == '02'}">selected</c:if>>批量上传</option>
								</select>
							</div>
						</div>
						
						<div id="uploadBox1" class="show">
							<div class="form-group clearfix">
								<label class="col-sm-2 control-label tr"><span
									style="color: red">*</span>&nbsp;域名：</label>
								<div class="col-sm-3">
									<textarea id="domain" name="domain" class="form-control"
										style="height: 200px"></textarea>
								</div>
							</div>
							<div class="form-group clearfix">
								<span style="color: red; padding-left: 100px">域名可以是主域名（如：www.baidu.com），也可以是2级域名（如：www.zhidao.baidu.com），域名可回车输入多个。</span>
								<br>
							</div>
							<div class="form-group clearfix">
								<label class="col-sm-2 control-label tr"><span
									style="color: red">*</span>&nbsp;关键词：</label>
								<div class="col-sm-3">
									<textarea id="keyWord" name="keyWord" class="form-control"
										style="height: 200px"></textarea>
								</div>
							</div>

							<div class="form-group clearfix">
								<span style="color: red; padding-left: 100px">关键词可回车输入多个。</span>
							</div>

							<div class="form-group clearfix">
								<a class="btn btn-primary" onclick="saveKeyWord()">保存</a>
							</div>
						</div>

						<div id="uploadBox2" class="hide">
							<div class="form-group clearfix">
								<label class="col-sm-2 control-label tr"><span
									style="color: red">*</span>&nbsp;域名关键词：</label>
								<div class="col-sm-3">
									<textarea id="urlKeyword" class="form-control"
										style="height: 300px" name="urlKeyword"></textarea>
								</div>
							</div>

							<div class="form-group clearfix">
								<span style="color: red; padding-left: 100px">每一行包含一个域名一个关键词，以空格分隔或直接从表格复制，例如（购物	www.taobao.com）</span>
							</div>

							<div class="form-group clearfix">
								<a class="btn btn-primary" onclick="saveKeyWord()">保存</a>
							</div>
						</div>
						<div class="hide" id="result">
							<div class="ibox float-e-margins">
 							<div class="ibox-content clearfix">
							<table class="table table-hover" id="resultTable">
                            	<tbody>
                            	</tbody>
                        	</table>
                        	</div>
                        	</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<script src="static/templet/js/jquery.min.js?v=2.1.4"></script>
	<script src="static/templet/js/bootstrap.min.js?v=3.3.5"></script>
	<script src="static/templet/js/plugins/peity/jquery.peity.min.js"></script>
	<script src="static/templet/js/plugins/layer/layer.min.js"></script>
	<script src="static/templet/js/iframe.min.js"></script>
	<script>
	function changeUpload(obj){
		var $this = $(obj);
		var uploadType = $("#uploadType option:selected").val();
		if(uploadType == "01"){
			$("#uploadBox1").removeClass("hide").addClass("show");
			$("#uploadBox2").removeClass("show").addClass("hide");
		}else{
			$("#uploadBox1").removeClass("show").addClass("hide");
			$("#uploadBox2").removeClass("hide").addClass("show");
		}
	}   
	    function saveKeyWord(){
	    	var resultTable = $("#resultTable");
	    	resultTable.find('tbody').html('');
	    	var engineCode = '';
	    	var domain = $("#domain").val();
	    	var keyWord = $("#keyWord").val();
	    	var userName = $("#userName").val();
	    	
	    	var uploadType = $("#uploadType option:selected").val();
	    	
	    	 $('input[name="engineCode"]:checked').each(function(){ 
	    		 engineCode+=$(this).val()+','; 
	    	 }); 
	    	 if(engineCode == '') {
	    		 alert("请选择搜索引擎");
	             return false;
	    	 }
	    	 
	    	 var data = {userName:userName,
	    	    		engineCode:engineCode,
	    	    		uploadType:uploadType};
	    	 
	    	 if(uploadType == "01"){
	    		 if ($.trim(domain) == "") {
		             alert("请输入域名");
		             return false;
		         }
		    	 if ($.trim(keyWord) == "") {
		             alert("请输入关键词");
		             return false;
		         }
		    	 data.domain = domain;
		    	 data.keyWord = keyWord;
	    	 }else{
	    		 var urlKeyword = $("#urlKeyword").val();
	    		 if ($.trim(urlKeyword) == "") {
	                 alert("请输入域名关键词");
	                 return false;
	             }
	    		 data.urlKeyword = urlKeyword;
	    	 }
	    	 
	    	
	    	var load = parent.layer.load(1, {
		        shade: [0.1,'#fff']
		    });
    	   
    	   $.ajax({
             type:"post",
             url:"keyWord/revision/add",
             data:data,
             traditional: true,
             success:function(data){
            	 var state = data.result.state;
            	 var successList = data.successList;
            	 var failList = data.failList;
            	 if(state == '1'){
            		 $.each(successList, function(n,v){
            			 var html = "<tr class='text-center'>"
            			 	 html +="<td>"+v.keyWord+"</td><td>"+v.domain+"</td><td>"+v.engineCodes+"</td>"
            			 	 html +="<td style='color:green'>成功</td></tr>"
            			 resultTable.find("tbody").append(html);
            		 })
					$.each(failList, function(n,v){
						var html = "<tr class='text-center'>"
           			 	 html +="<td>"+v.keyWord+"</td><td>"+v.domain+"</td><td>"+v.engineCodes+"</td>"
           			 	 html +="<td style='color:red'>导入重复</td></tr>"
           			 	resultTable.find("tbody").append(html);
            		 })
            		 
            		 parent.layer.open({
            	    	    type: 1,
            	    	    title: '添加信息',
            	    	    shadeClose: true,
            	    	    shade: 0.8,
            	    	    area: ['80%', '60%'],
            	    	    content: $("#result").html(),
            	    	    cancel:function(index, layero){
            	    	    	iframe.get(layero);
            	    	    	if(result == 1){
            	    	    		location.reload(true);
            	    	    	}
            	    	    	
            	    	    }
            	    	});
            		 location.reload(); 
            	 }else{
            		 var message = data.result.message;
            		 parent.layer.msg(message, {icon: 1});
            	 }
            	 parent.layer.close(load);
 			 },
 			 error:function(v,r) {
 				 parent.layer.msg("网络繁忙", {icon: 2});
 				parent.layer.close(load);
 			 }
 　　                 })
    	   
	    }
    </script>
</body>

</html>
